<template>
  <div v-if="visible" class="message-modal-overlay" @click="handleOverlayClick">
    <div class="message-modal" @click.stop>
      <div class="modal-header">
        <div class="modal-icon" :class="iconClass">
          <span>{{ iconText }}</span>
        </div>
        <h3 class="modal-title">{{ title }}</h3>
        <button class="close-btn" @click="close">
          <span>&times;</span>
        </button>
      </div>
      
      <div class="modal-body">
        <p class="modal-message">{{ message }}</p>
      </div>
      
      <div class="modal-footer">
        <button 
          v-if="type === 'confirm'"
          class="btn btn-secondary"
          @click="cancel"
        >
          {{ cancelText }}
        </button>
        <button 
          class="btn"
          :class="confirmButtonClass"
          @click="confirm"
        >
          {{ confirmText }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';

// Props
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  type: {
    type: String,
    default: 'info', // info, success, warning, error, confirm
    validator: (value) => ['info', 'success', 'warning', 'error', 'confirm'].includes(value)
  },
  title: {
    type: String,
    default: '提示'
  },
  message: {
    type: String,
    required: true
  },
  confirmText: {
    type: String,
    default: '确定'
  },
  cancelText: {
    type: String,
    default: '取消'
  },
  autoClose: {
    type: Boolean,
    default: false
  },
  autoCloseDelay: {
    type: Number,
    default: 3000
  }
});

// Emits
const emit = defineEmits(['close', 'confirm', 'cancel']);

/**
 * 图标样式类
 */
const iconClass = computed(() => {
  const classMap = {
    'info': 'icon-info',
    'success': 'icon-success',
    'warning': 'icon-warning',
    'error': 'icon-error',
    'confirm': 'icon-confirm'
  };
  return classMap[props.type] || 'icon-info';
});

/**
 * 图标文本
 */
const iconText = computed(() => {
  const iconMap = {
    'info': 'ℹ️',
    'success': '✅',
    'warning': '⚠️',
    'error': '❌',
    'confirm': '❓'
  };
  return iconMap[props.type] || 'ℹ️';
});

/**
 * 确认按钮样式类
 */
const confirmButtonClass = computed(() => {
  const classMap = {
    'info': 'btn-primary',
    'success': 'btn-success',
    'warning': 'btn-warning',
    'error': 'btn-danger',
    'confirm': 'btn-primary'
  };
  return classMap[props.type] || 'btn-primary';
});

/**
 * 关闭弹窗
 */
const close = () => {
  emit('close');
};

/**
 * 确认操作
 */
const confirm = () => {
  emit('confirm');
  close();
};

/**
 * 取消操作
 */
const cancel = () => {
  emit('cancel');
  close();
};

/**
 * 处理遮罩层点击
 */
const handleOverlayClick = () => {
  if (props.type !== 'confirm') {
    close();
  }
};

// 自动关闭
if (props.autoClose && props.type !== 'confirm') {
  setTimeout(() => {
    if (props.visible) {
      close();
    }
  }, props.autoCloseDelay);
}
</script>

<style scoped>
.message-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  backdrop-filter: blur(2px);
}

.message-modal {
  background: white;
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  min-width: 400px;
  max-width: 500px;
  max-height: 80vh;
  overflow: hidden;
  animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translateY(-50px) scale(0.9);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-header {
  padding: 20px 24px 16px;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  align-items: center;
  gap: 12px;
  position: relative;
}

.modal-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.icon-info {
  background: #e6f7ff;
  color: #1890ff;
}

.icon-success {
  background: #f6ffed;
  color: #52c41a;
}

.icon-warning {
  background: #fff7e6;
  color: #fa8c16;
}

.icon-error {
  background: #fff2f0;
  color: #ff4d4f;
}

.icon-confirm {
  background: #f0f5ff;
  color: #722ed1;
}

.modal-title {
  flex: 1;
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #262626;
}

.close-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  font-size: 24px;
  color: #8c8c8c;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.2s;
}

.close-btn:hover {
  background: #f5f5f5;
  color: #595959;
}

.modal-body {
  padding: 16px 24px 24px;
}

.modal-message {
  margin: 0;
  font-size: 14px;
  line-height: 1.6;
  color: #595959;
}

.modal-footer {
  padding: 16px 24px 24px;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.btn {
  padding: 8px 16px;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
  min-width: 80px;
}

.btn-primary {
  background: #1890ff;
  border-color: #1890ff;
  color: white;
}

.btn-primary:hover {
  background: #40a9ff;
  border-color: #40a9ff;
}

.btn-success {
  background: #52c41a;
  border-color: #52c41a;
  color: white;
}

.btn-success:hover {
  background: #73d13d;
  border-color: #73d13d;
}

.btn-warning {
  background: #fa8c16;
  border-color: #fa8c16;
  color: white;
}

.btn-warning:hover {
  background: #ffa940;
  border-color: #ffa940;
}

.btn-danger {
  background: #ff4d4f;
  border-color: #ff4d4f;
  color: white;
}

.btn-danger:hover {
  background: #ff7875;
  border-color: #ff7875;
}

.btn-secondary {
  background: white;
  border-color: #d9d9d9;
  color: #595959;
}

.btn-secondary:hover {
  background: #f5f5f5;
  border-color: #b7b7b7;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .message-modal {
    min-width: 320px;
    margin: 20px;
  }
  
  .modal-header {
    padding: 16px 20px 12px;
  }
  
  .modal-body {
    padding: 12px 20px 20px;
  }
  
  .modal-footer {
    padding: 12px 20px 20px;
  }
}
</style>